#! /bin/sh

name=tzdata
version=2025b
revision=1
tarball_url="https://data.iana.org/time-zones/releases/tzdata${version}.tar.gz"
tarball_blake2b="07421e756cd33f003f1173c320a2e454f767a2ba299b76b54e34dc5cf1c126b00ab3186bc61d4989201a83cb8b3e4c005c0bc080fb39a1e48ff9555d50c84f1d"
source_allow_network="yes"
imagedeps="tzdata"
hostdeps="gcc binutils"
deps="mlibc libgcc libstdc++ libatomic libintl libiconv"

prepare() {
    curl -Lo tzcode.tar.gz "https://data.iana.org/time-zones/releases/tzcode${version}.tar.gz"
    tar -xf tzcode.tar.gz
    sed -i 's/sbin/bin/g' Makefile
}

build() {
    cp -rp ${source_dir}/. ./

    make \
        CC="$OS_TRIPLET-gcc" \
        AR="$OS_TRIPLET-ar" \
        CFLAGS="$TARGET_CFLAGS -std=c99 $TARGET_CPPFLAGS" \
        LDFLAGS="$TARGET_LDFLAGS" \
        LDLIBS="-lintl"
}

package() {
    make DESTDIR="${dest_dir}" ZIC=zic install

    timezones="africa antarctica asia australasia europe northamerica southamerica etcetera backward factory"

    # Create the required directories
    mkdir -p "${dest_dir}"/etc
    mkdir -p "${dest_dir}"/usr/share/zoneinfo/posix
    mkdir -p "${dest_dir}"/usr/share/zoneinfo/right

    # Create the time zone files without leap seconds, convention puts these in both zoneinfo and zoneinfo/posix.
    # After that. create time time zone files with leap seconds
    zic -b fat -d "${dest_dir}"/usr/share/zoneinfo $timezones
    zic -b fat -d "${dest_dir}"/usr/share/zoneinfo/posix $timezones
    zic -b fat -d "${dest_dir}"/usr/share/zoneinfo/right -L leapseconds $timezones

    # Create the posixrules file, POSIX requires daylight saving rules to be in accordance with US rules, thus use New York
    zic -b fat -d "${dest_dir}"/usr/share/zoneinfo -p America/New_York

    # Default to UTC for localtime.
    ln -sf /usr/share/zoneinfo/UTC "${dest_dir}"/etc/localtime

    install -m644 -t "${dest_dir}"/usr/share/zoneinfo iso3166.tab leap-seconds.list zone1970.tab zone.tab SECURITY

    post_package_strip
}
